package com.torch.demo03;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//用这个类，自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的接口
    private Object proxyInterface;
    private AddRequire addRequire;

    public void setAddRequire(AddRequire addRequire) {
        this.addRequire = addRequire;
    }

    public void setProxyInterface(Object proxyInterface) {
        this.proxyInterface = proxyInterface;
    }

    //生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), proxyInterface.getClass().getInterfaces(),this);
    }

    //处理代理实例，并返回结果
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质，就是使用反射机制实现！
        addRequire.addRequireBeforeOriginalMethod();
        Object result = method.invoke(proxyInterface,args);
        addRequire.addRequireAfterOriginalMethod();
        return result;
    }
}
